﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading.Tasks;
using System.Threading;
using ConsoleApplication27;

namespace WpfApplication11
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }



        private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {
            button3.IsEnabled = true;
            button2.IsEnabled = true;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            button5.Visibility = Visibility.Visible;
            button6.Visibility = Visibility.Visible;

        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
           /* Liczba xxx = new Liczba("132 71 45 20 103 168 216 55 56 238 165 101 220 88 13 219 142 126 67 178 145 147 157 40 177 75 129 38 157 186 67 141 92 161 24 189 242 67 54 13 229 195 166 188 222 14 150 212 135 212 139 162 92 229 101 136 202 188 248 54 194 228 103 251", 256);
            Liczba xxx2 = new Liczba("1 0 255", 256);
            List<Liczba> trololol = PotegowanieModulo.ToBin(xxx);
            Liczba wynikk=xxx % xxx2;
            wynikk = RozszerzonyAlgorytmEuklidesa.zrob(xxx, xxx2); */


            ulong system = 256; //system

            Liczba a;


            if (radioButton1.IsChecked == true) a = new Liczba("255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255", system);
            else
                if (radioButton2.IsChecked == true) a = new Liczba("255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255", system);
                else
                    a = new Liczba("255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255", system);

            a.znak = '+';

            Random r = new Random();
            RabinMiller rabin = new RabinMiller();

            rabin.primes = new List<Liczba>();

            int i = 0;
            DateTime before = DateTime.Now;

            while (rabin.primes.Count < 2) { rabin.test(Pomoce.randomodd(a, r), r); Console.WriteLine(i++); }

            RSA.makekeys(rabin.primes[0], rabin.primes[1]);


        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {

            ulong system = 256; //system

            Liczba a;


            if (radioButton1.IsChecked == true) a = new Liczba("255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255", system);
            else
                if (radioButton2.IsChecked == true) a = new Liczba("255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255", system);
                else
                    a = new Liczba("255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255", system);

            a.znak = '+';



            RabinMiller rabin = new RabinMiller();
            int i = 0;
            Random r = new Random();

            Action<ParallelLoopState> processQueue = (lps) =>
            {
                rabin.test(Pomoce.randomodd(a, r), r);
                i++;

            };

            Func<bool> dwiepierwsze = () => rabin.primes.Count < 2;


            processQueue.InParallelWhile(dwiepierwsze);

            RSA.makekeys(rabin.primes[0], rabin.primes[1]);


        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {

            io plik = new io();
            Microsoft.Win32.OpenFileDialog open = new Microsoft.Win32.OpenFileDialog();
            bool result = (bool)open.ShowDialog();

            List<Liczba> zakodowe = RSA.szyfruj(open.FileName);

            Microsoft.Win32.SaveFileDialog save = new Microsoft.Win32.SaveFileDialog();
            result = (bool)save.ShowDialog();
            plik.zapisz(zakodowe, save.FileName);
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            io plik = new io();
            Microsoft.Win32.OpenFileDialog open = new Microsoft.Win32.OpenFileDialog();
            bool result = (bool)open.ShowDialog();

            List<Liczba> odszyfrowane = RSA.odszyfruj(open.FileName);

            Microsoft.Win32.SaveFileDialog save = new Microsoft.Win32.SaveFileDialog();
            result = (bool)save.ShowDialog();
            plik.zapisz(odszyfrowane, save.FileName);




        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            io plik = new io();
            Microsoft.Win32.OpenFileDialog open = new Microsoft.Win32.OpenFileDialog();
            bool result = (bool)open.ShowDialog();
            plik.wczytajklucze(open.FileName);


        }

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            Window1 win = new Window1();
            win.Show();
        }
    }
}
